package org.senseframework.support.boot.application;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * Created by lanjian on 2016/12/16.
 */
public class ApplicationRun {
    private static Log log = LogFactory.getLog(ApplicationRun.class);

    private static void httpServer(int port) {
        try {
            Server server = new Server(port);
            DispatcherServlet servlet = new DispatcherServlet();
            servlet.setContextConfigLocation("classpath:servlet-context.xml");
            XmlWebApplicationContext wctx = new XmlWebApplicationContext();
            wctx.setConfigLocation("");
            wctx.setServletContext(servlet.getServletContext());
            wctx.refresh();
            ServletContextHandler context = new ServletContextHandler();
            context.setContextPath("/");
            context.addServlet(new ServletHolder("pluginServlet", servlet), "/");
            context.setAttribute(XmlWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wctx);
            HandlerCollection handlers = new HandlerCollection();
            handlers.setHandlers(new Handler[]{context, new DefaultHandler()});
            server.setHandler(handlers);
            server.start();
            server.join();
        } catch (Exception e) {
            log.error("httpServer",e);
        }
    }

    public static void run(Class cls, String[] args) {
        //应用程序启动入口

    }
}
